package ontology.concepts.vo;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;

import org.hibernate.annotations.IndexColumn;

import ontology.concepts.general.ICase;

@Entity
@NamedQueries(
		{
			@NamedQuery(name = "VOCase.findVOCaseByNorm", query = "SELECT c from VOCase c WHERE c.norm.name = :normName"),
			@NamedQuery(name = "VOCase.findAll", query = "SELECT c from VOCase c"),
			@NamedQuery(name = "VOCase.findVOCaseByNormAction", query = "SELECT c from VOCase c WHERE c.violation.voaction.name = :actionName"),
			@NamedQuery(name = "VOCase.findVOCaseByNormActionArgs", query = "SELECT c from VOCase c, Antecedent ant, Situation sit WHERE sit.voaction.args = :actionArgs"),
			@NamedQuery(name = "VOCase.findVOCaseByContractType", query = "SELECT c from VOCase c WHERE c.norm.contract_type = :contractType")
		}
)
public class VOCase implements ICase<Integer>{
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	
	@ElementCollection(targetClass = String.class, fetch=FetchType.EAGER)
	@JoinTable(name = "vocase_parties",
			joinColumns = @JoinColumn(name = "id"))
	@IndexColumn(name = "position", base = 0)
	private List<String> parties; //partes envolvidas no conflito

	@OneToOne(cascade= CascadeType.ALL, fetch = FetchType.EAGER)
	private VOObligation violation;
	
	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private VONorm norm; //The norm that prescribes the obligation that has been violated

	@Override
	public void setID(Integer id) {
		this.id = id;
	}
	
	@Override
	public Integer getID() {
		return id;
	}

	public List<String> getParties() {
		return parties;
	}

	public void setParties(List<String> parties) {
		this.parties = parties;
	}	
	
	public VOObligation getViolation() {
		return violation;
	}

	public void setViolation(VOObligation violation) {
		this.violation = violation;
	}

	public VONorm getNorm() {
		return norm;
	}

	public void setNorm(VONorm norm) {
		this.norm = norm;
	}

	
	
	@Override
	public String toString() {
		if(parties == null)
			System.out.println("Null nas parties");
		if(norm == null)
			System.out.println("Null na norma");
		return "VOCase [id=" + id + ", parties=" + parties + ", norm=" + norm.toString()
				+ "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((norm == null) ? 0 : norm.hashCode());
		result = prime * result + ((parties == null) ? 0 : parties.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		VOCase other = (VOCase) obj;
		if (id != other.id)
			return false;
		if (norm == null) {
			if (other.norm != null)
				return false;
		} else if (!norm.equals(other.norm))
			return false;
		if (parties == null) {
			if (other.parties != null)
				return false;
		} else if (!parties.equals(other.parties))
			return false;
		return true;
	}

}
